package 简单算法;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2019-05-16
 * @Description: 给定一个字符串（以字符数组的形式给出）和一个偏移量，根据偏移量原地旋转字符串(从左向右旋转)
 **/
public class 旋转字符串 {
    public static void main(String[] args) {
        rotateString("abcdefg".toCharArray(), 3);
    }

    public static void rotateString(char[] str, int offset) {
        // write your code here
        if (str == null || str.length == 0) {
            return;
        }
        offset = offset % str.length;
//        reverse(str, 0, str.length - offset - 1);
//        reverse(str, str.length - offset, str.length - 1);
//        reverse(str, 0, str.length - 1);
        for (int i = 0; i < offset; i++) {
            char temp = str[str.length - 1];
            for (int j = str.length - 1; j > 0; j--) {
                str[j] = str[j - 1];
            }
            str[0] = temp;
        }
        System.out.println(str);
        return;

    }

    private static void reverse(char[] str, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            char temp = str[i];
            str[i] = str[j];
            str[j] = temp;
        }
    }
}
